from .base import db, BaseModel
import uuid
from datetime import datetime
from .user_models import UUIDString

class MediaFile(BaseModel):
    """媒体文件模型，存储加密后的媒体文件元数据"""
    __tablename__ = 'media_files'

    id = db.Column(UUIDString, primary_key=True, default=lambda: str(uuid.uuid4()))
    user_id = db.Column(UUIDString, db.ForeignKey('users.id'), nullable=False)
    filename = db.Column(db.String(255), nullable=False)
    file_path = db.Column(db.String(512), nullable=False)
    file_key = db.Column(db.String(64), nullable=False)  # 加密密钥(hex格式)
    nonce = db.Column(db.String(32), nullable=False)     # AES-GCM nonce(hex格式)
    tag = db.Column(db.String(32), nullable=False)      # AES-GCM tag(hex格式)
    content_type = db.Column(db.String(128))
    size = db.Column(db.BigInteger)
    uploaded_at = db.Column(db.DateTime, default=datetime.utcnow)
    is_deleted = db.Column(db.Boolean, default=False)

    # 关联用户
    user = db.relationship('User', backref=db.backref('media_files', lazy=True))

    def __repr__(self):
        return f'<MediaFile {self.filename}>'